package com.rbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rbs.entity.User;
import com.rbs.service.UserService;
import com.rbs.util.JwtUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @GetMapping("/login")
    public Map<String,String> login(String username,String pwd){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userName",username).eq("userPassword",pwd);
        User user = userService.getOne(wrapper);
        Map<String,String> map = new HashMap<>();
        if (user == null){
            map.put("msg","登录失败");
        }else {
            map.put("msg","登录成成功");
            Map<String,String> jwtMap = new HashMap<>();
            jwtMap.put("userId",user.getId()+"");
            jwtMap.put("name",username);
            jwtMap.put("role",user.getUserIsAdmin()+"");
            String token = JwtUtil.getToken(jwtMap);
            map.put("token",token);
        }
        return map;
    }

    @GetMapping("/selectOne/{id}")
    public User selectOne(@PathVariable("id") int id){
        return userService.getById(id);
    }

}
